package com.ruoyi.salesInvoice.invoiceStatusMachine.impl;

import com.ruoyi.audit.domain.Audit;
import com.ruoyi.audit.mapper.AuditMapper;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.salesInvoice.domain.SalesInvoiceModel;
import com.ruoyi.salesInvoice.domain.SalesInvoiceOrder;
import com.ruoyi.salesInvoice.invoiceStatusMachine.InvoiceService;
import com.ruoyi.salesInvoice.mapper.SalesInvoiceMapper;
import com.ruoyi.salesInvoice.service.SalesInvoiceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 发票状态 驳回
 * @Author Mr.Yi.Zhang
 * @Version 1.0
 */
@Component
public class InvoiceServiceReject implements InvoiceService {

    private static final String FLAG = "2";

    @Override
    public String get() {
        return FLAG;
    }

    /** 发票订单Service */
    private SalesInvoiceMapper salesInvoiceMapper;
    /** 销售审核Mapper */
    private AuditMapper auditMapper;
    /***/
    private SaleGoodsMapper saleGoodsMapper;
    public InvoiceServiceReject(
            SalesInvoiceMapper salesInvoiceMapper,
            AuditMapper auditMapper,
            SaleGoodsMapper saleGoodsMapper
    ) {
      this.salesInvoiceMapper = salesInvoiceMapper;
      this.auditMapper = auditMapper;
      this.saleGoodsMapper = saleGoodsMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int execute(SalesInvoiceOrder order) {
        /// =============同步销售审核状态=============== ///
        Audit auditQueryParams = new Audit();
        auditQueryParams.setOrderId(order.getSalesInvoiceId());
        auditQueryParams.setOrderType("7");
        Audit audit = auditMapper.selectAuditByOrderId(auditQueryParams);
        audit.setAuditStatus("2");
        auditMapper.updateAudit(audit);

        // salesInvoiceService.countReturnSalesGoods(order.getSalesInvoiceId());
        List<SalesInvoiceModel> salesInvoiceModels = salesInvoiceMapper.selectSalesInvoiceModelByOrderId(order.getSalesInvoiceId());
        ArrayList<Long> longs = new ArrayList<>();
        for (SalesInvoiceModel salesInvoiceModel : salesInvoiceModels) {
            longs.add(salesInvoiceModel.getSaleGoodsModelId());
        }
        List<SaleGoodsModel> saleGoodsModels = saleGoodsMapper.selectSaleGoodsModelByLists(longs);
        List<SaleGoodsModel> saleGoodsModelList = new ArrayList<SaleGoodsModel>();
        for (int i = 0; i < salesInvoiceModels.size(); i++) {
            //销售发票中销售子表信息
            SalesInvoiceModel salesInvoiceModel = salesInvoiceModels.get(i);
            //销售发票中销货单子表主键,销售发票开票数量
            Long saleGoodsModelId = salesInvoiceModel.getSaleGoodsModelId();
            int salesInvoiceModelNum = salesInvoiceModel.getAllNum().intValue();
            for (int j = saleGoodsModels.size() - 1; j >= 0; j--) {
                //销货单子表信息
                SaleGoodsModel saleGoodsModel = saleGoodsModels.get(j);
                //销货单子表中的销货单子表主键
                Long saleGoodsModelId1 = saleGoodsModel.getSaleGoodsModelId();
                //执行数量
                int executeNum = saleGoodsModel.getExecuteNum();

                //执行状态
                String executeStatus = saleGoodsModel.getExecuteStatus();

                //剩余数量
                int remainingNum = saleGoodsModel.getRemainingNum();
                //销货单数量
                int saleGoodsModelsNum = saleGoodsModel.getNum();

                if (saleGoodsModelId.equals(saleGoodsModelId1)) {
                    remainingNum += salesInvoiceModelNum;
                    if (remainingNum == 0) {
                        executeStatus = "1";
                    } else if (remainingNum == saleGoodsModelsNum) {
                        executeStatus = "0";
                    } else {
                        executeStatus = "2";
                    }
                    executeNum -= salesInvoiceModelNum;
                    saleGoodsModel.setRemainingNum(remainingNum);
                    saleGoodsModel.setExecuteNum(executeNum);
                    saleGoodsModel.setExecuteStatus(executeStatus);

                    saleGoodsModelList.add(saleGoodsModel);
                }
            }
        }
        return salesInvoiceMapper.updateSalesInvoiceOrder(order);
    }
}
